package com.home.cop.employee.controller;

import com.home.cop.base.BaseController;
import com.home.cop.employee.model.Employee;
import com.home.cop.employee.service.EmployeeService;
import com.home.cop.sysinfo.model.UserSession;
import com.home.cop.system.RequiredAuth;
import com.home.cop.system.Result;
import com.home.cop.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/employee")
@Api("员工相关")
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;

    @RequiredAuth
    @PostMapping("/list")
    @ApiOperation(notes = "查询员工列表",value = "根据条件查询员工列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "instName", value = "机构名字", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "jobId", value = "职务编号", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-启用，1-禁用", required = false, dataType = "Integer", paramType = "query")
    })
    public Result list(@RequestParam(value = "instName",required = false)String instName,
                       @RequestParam(value = "name",required = false)String name,
                       @RequestParam(value = "startTime",required = false)String startTime,
                       @RequestParam(value = "endTime",required = false)String endTime,
                       @RequestParam(value = "roleId",required = false)Integer roleId,
                       @RequestParam(value = "status",required = false)Integer status,
                       @RequestParam(value = "jobId",required = false)Integer jobId){
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(instName)){
            params.put("instName",instName.trim());
        }
        if (StringUtils.isNotBlank(name)){
            params.put("name",name.trim());
        }
        if (StringUtils.isNotBlank(endTime)){
            params.put("endTime",endTime);
        }
        if (StringUtils.isNotBlank(startTime)){
            params.put("startTime",startTime.trim());
        }
        if (roleId != null){
            params.put("roleId",roleId);
        }
        if (status != null){
            params.put("status",status);
        }
        if (jobId != null){
            params.put("jobId",jobId);
        }
        /*普通员工只能看到自己的信息*/
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            params.put("account",userSession.getAccount());
        }
        return Result.returnSuccess().setData(employeeService.list(params,getPageNum(),getPageSize()));
    }

    @RequiredAuth
    @PostMapping("/add")
    @ApiOperation(notes = "新增员工",value = "超级管理员新增员工")
    public Result addEmployee(@RequestBody Employee employee){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        employeeService.insert(employee);
        return Result.returnSuccess().setMessage("新增成功！");
    }

    @RequiredAuth
    @PostMapping("/updateInfo")
    @ApiOperation(notes = "修改信息",value = "修改信息")
    public Result updateInfo(@RequestBody Employee employee){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        employeeService.updateInfo(employee);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @RequiredAuth
    @PostMapping("/upateStatus")
    @ApiOperation(notes = "修改状态",value = "修改员工状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-启用，1-禁用", required = true, dataType = "Integer", paramType = "query")
    })
    public Result upateStatus(@RequestParam("account") String account,@RequestParam("status")Integer  status){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        employeeService.updateStatus(account,status);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @RequiredAuth
    @PostMapping("/delete")
    @ApiOperation(notes = "删除员工",value = "删除员工")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String", paramType = "query")
    })
    public Result deleteEmployee(@RequestParam("account") String account){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        if (account.equals(userSession.getAccount())){
            return Result.returnFail().setMessage("不能删除自己！");
        }
        employeeService.deleteEmployee(account);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @RequiredAuth
    @PostMapping("/upatePass")
    @ApiOperation(notes = "修改密码",value = "修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String", paramType = "query")
    })
    public Result upatePass(@RequestParam("account") String account){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        String pass = RandomStringUtils.randomAlphanumeric(10);
        employeeService.updatePass(account,MD5Utils.MD5(pass));
        return Result.returnSuccess().setData(pass);
    }

    @RequiredAuth
    @PostMapping("/listByInstId")
    @ApiOperation(notes = "根据机构编号获取员工",value = "根据机构编号获取员工，只返回账号和姓名")
    @ApiImplicitParams({@ApiImplicitParam(name = "instId", value = "机构编号", required = true, dataType = "Integer", paramType = "query")
    })
    public Result listByInstId(@RequestParam(value = "instId",required = false) Integer instId){
        if (instId == null){
            UserSession userSession = getUserSession();
            instId = userSession.getInstId();
        }
        return Result.returnSuccess().setData(employeeService.getEmployeeByInstId(instId));
    }
}
